package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LocalAlbumAdapter;
import flc.ast.adapter.LocalAlbumItemAdapter;
import flc.ast.bean.MyAlbumItemBean;
import flc.ast.bean.MyLocalAlbumBean;
import flc.ast.databinding.ActivityLocalAlbumBinding;
import flc.ast.utils.MhzDateUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class LocalAlbumActivity extends BaseAc<ActivityLocalAlbumBinding> {

    private LocalAlbumAdapter albumAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_local_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.tvLocalAlbumBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvAlbumtList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        albumAdapter = new LocalAlbumAdapter();
        mDataBinding.rvAlbumtList.setAdapter(albumAdapter);
        albumAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof LocalAlbumItemAdapter) {
            LocalAlbumItemAdapter albumItemAdapter = (LocalAlbumItemAdapter) adapter;
            ImgFileDetailActivity.imgPath = albumItemAdapter.getItem(position).getPath();
            startActivity(ImgFileDetailActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_local_album_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvAlbumtList.setVisibility(View.GONE);
                        mDataBinding.ivAlbumNotData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        showDialog(getString(R.string.get_data_ing));
        RxUtil.create(new RxUtil.Callback<List<MyLocalAlbumBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MyLocalAlbumBean>> emitter) {
                List<String> list = new ArrayList<>();
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                for (SelectMediaEntity bean : listSys) {
                    list.add(bean.getPath());
                }
                List<MyLocalAlbumBean> dayData = MhzDateUtils.getDayData(list);
                emitter.onNext(dayData);
            }

            @Override
            public void accept(List<MyLocalAlbumBean> list) {
                dismissDialog();
                if (list != null && list.size() > 0) {
                    albumAdapter.setList(list);
                    mDataBinding.rvAlbumtList.setVisibility(View.VISIBLE);
                    mDataBinding.ivAlbumNotData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAlbumtList.setVisibility(View.GONE);
                    mDataBinding.ivAlbumNotData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}